﻿//鼠标事件
var MouseOperation = function () {
    var isActivityMove = false;
    var isRuleMove = false;

};

MouseOperation.prototype = {
    //leftbuttondown  单击
    leftbuttondown: function (e) {
        $.each(activityHelper.Activitys, function (i, activity) { activity.isSelect = 0; });
        $.each(rules, function (a, val) { val.isSelect = false; })
        //添加节点，
        if (gcommand == Command.AddActivity) {
            activityHelper.AddActivity(canvasHelper.GetPoint(e), currentActivityType);

        }
        else if (gcommand == Command.AddRules) { }
            //选择操作
        else {

            //1、获得可能选中的节点
            var result = canvasHelper.GetSelectedActivity(e);
            /*result.isSelect：true 有节点被选中
                               false没有节点被选中  
            */
            if (result.isSelect) {
                result.selectedObject.isSelect = 3;

                if (selectedObjects.length > 0 && selectedObjects.contains(result.selectedObject)) {
                    $.each(selectedObjects, function (i, val) {
                        val.currentActivityPosition = val.position;
                        val.startMovePoint = canvasHelper.GetPoint(e);
                    });
                    moveActivity = true;
                }

                else {
                    //清除样式
                    canvasHelper.ClearAllStyle();
                    result.selectedObject.SelectActivity(e, result.distinct);
                }
            }

            else {

                selectedObjects = new Array();
                result = canvasHelper.GetSelectedRule(e);
                //1、是否有规则被选中
                if (result.isSelect) {
                    $("#divWorkspace").css("cursor", "pointer");
                    result.selectedObject.isSelect = true;
                    currentRule = result.selectedObject;
                    currentRule.SelectRule(result);
                    canvasHelper.ClearAllStyle();
                }
                else {
                    //没有任何单元被选中
                    canvasHelper.ClearAllStyle();
                    gcommand = Command.NullOperation;

                }
            }
        }
    },

    //鼠标左键弹起事件
    leftbuttonUp: function (e) {
        var me = this;
        if (!e.ctrlKey) {
            selectedObjects = new Array();
        }
        var result = canvasHelper.GetSelectedActivity(e);
        switch (gcommand) {
            case Command.AddRules:
                if (result.isSelect) {
                    if (result.selectedObject != currentRule.startActivity && result.selectedObject.CanHaveToRule()) {
                        canvasHelper.AddRule(currentRule, result.selectedObject);
                    }
                }
                else
                    currentRule.AddPoint(canvasHelper.GetPoint(e));
                break;
            case "Approval":
                gcommand = Command.NullOperation;
                break;
            case Command.SelectActivity:
                moveActivity = false;
                if (currentActivity.capture.iscapture)
                    currentActivity.MoveActivity(currentActivity.capture.point, 'tra');
                if (me.isActivityMove) {
                    me.isActivityMove = false;
                    ToolBar.HistroyOpertion();
                }
                currentActivity.ResetActivity();
                break;
        }
        if (moveRule) {
            currentRule.ChangeActivity(canvasHelper.GetPoint(e), result);
            if (me.isRuleMove) {
                ToolBar.HistroyOpertion();
                isRuleMove = false;
            }
            moveRule = false;
        }
        $("#divWorkspace").css("cursor", "default");

    },
    //鼠标移动
    mouseMove: function (e) {


        if (document.getBoxObjectFor) {
            alert("");
        }


        var me = this;
        var point = canvasHelper.GetPoint(e);
        if (gcommand == Command.AddRules) {
            if (!currentRule.startActivity.Picture.isPointInPath(e.offsetX, e.offsetY)) {
                var currentPoint = canvasHelper.GetPoint(e);
                if (currentRule.capture.iscapture)
                    currentPoint = currentRule.capture.point
                currentRule.DrawRule(currentPoint);
                currentRule.DrawReferenceLine(point);
            }
        } else if (gcommand == Command.AddActivity) {

        } else if (moveActivity) {
            if (selectedObjects.length == 0) {
                currentActivity.MoveActivity(point);

                currentActivity.DrawReferenceLine(point);
                me.isActivityMove = true;
            }
            else {
                $.each(selectedObjects, function (i, val) {
                    val.MoveActivity(point);
                });
            }
        }
        else if (moveRule) {
            currentRule.ResetPoint(e);
            me.isRuleMove = true;
        }
        else {
            var result = canvasHelper.GetSelectedActivity(e);
            $.each(activityHelper.Activitys, function (i, activity) {
                if (activity.isSelect < 3)
                    activity.isSelect = 0;
                activity.isDrawRuleStart = false;
            });

            if (result.isSelect) {

                if (result.distinct > 10) {
                    $("#divWorkspace").css("cursor", "pointer");
                    if (result.selectedObject.isSelect != 3)
                        result.selectedObject.isSelect = 1;
                }
                else if (result.selectedObject.CanHaveFromRule()) {
                    $("#divWorkspace").css("cursor", "crosshair");
                    result.selectedObject.isDrawRuleStart = true;
                    if (result.selectedObject.isSelect != 3)
                        result.selectedObject.isSelect = 2;
                }

            }
            else {
                $.each(activityHelper.Activitys, function (i, activity) {
                    if (activity.isSelect < 3)
                        activity.isSelect = 0;
                });
                result = canvasHelper.GetSelectedRule(e);
                if (result.isSelect)
                    $("#divWorkspace").css("cursor", "pointer");
                else
                    $("#divWorkspace").css("cursor", "default");
            }
            canvasHelper.ClearAllStyle();
        }
    },
    //鼠标右键
    rightbuttondown: function (e) {
        if (gcommand == Command.AddRules) {
            currentRule.Dispose();
            gcommand = Command.NullOperation;
        } else if (gcommand == Command.AddActivity) {
            gcommand = Command.NullOperation;

        }

        return false;
    },

    ctrlKeybuttondown: function (e) {

        if (selectedObjects.length == 0 && gcommand == Command.SelectActivity) {
            selectedObjects[0] = currentActivity;
        }
        var result = canvasHelper.GetSelectedActivity(e);
        if (result.isSelect) {
            selectedObjects[selectedObjects.length] = result.selectedObject;
            result.selectedObject.SelectActivity(e, result.distinct);
            $.each(selectedObjects, function (i, val) {
                val.currentActivityPosition = val.position;
                val.startMovePoint = canvasHelper.GetPoint(e);
            });
        }
    },
    //左键双击
    leftbuttondoubleclick: function (e) {
        var result = canvasHelper.GetSelectedActivity(e);
        if (result.isSelect) {
            if (gcommand == Command.AddRules)
                this.rightbuttondown();
            //显示节点属性
            result.selectedObject.ShowProperty();

        }
        else {
            result = canvasHelper.GetSelectedRule(e);
            if (result.isSelect) {
                //显示规则线属性
                result.selectedObject.ShowProperty();
            }
        }
        return false;
    }
}
